/*
 * $HeadURL:  $
 *
 * Copyright (c) 2007 Drutt Corporation, all rights reserved.
 *
 */
package homework.james.xiao.server;

import homework.james.xiao.bean.Waiter;
import homework.james.xiao.util.Util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server_Socket {
	public static int PORT = 1234;
	private ServerSocket serverSocket;
    private ExecutorService executorService;//线程池
    private final int POOL_SIZE=10;//单个CPU线程池大小
    
    public Server_Socket() throws IOException{
        serverSocket=new ServerSocket(PORT);
        //Runtime的availableProcessor()方法返回当前系统的CPU数目.
        executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
        System.out.println("starting server");
    }
    public void service(){
        while(true){
            Socket socket=null;
            try {
                //接收客户连接
                socket=serverSocket.accept();
                executorService.execute(new waiterThread(socket));                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    
	public static void main(String args[]) throws IOException {
		if (args.length > 0) {
			PORT = Integer.valueOf(args[0]);
		}
		new Server_Socket().service();
	}
	
	class waiterThread implements Runnable{
		private Socket socket;
	    public waiterThread(Socket socket){
	        this.socket=socket;
	    }
	    
	    private PrintWriter getWriter(Socket socket) throws IOException{
	        OutputStream socketOut=socket.getOutputStream();
	        return new PrintWriter(socketOut,true);
	    }
	    private BufferedReader getReader(Socket socket) throws IOException{
	        InputStream socketIn=socket.getInputStream();
	        return new BufferedReader(new InputStreamReader(socketIn));
	    }
	    
		/**
		 * 处理用户请求
		 */
		public void run() {
			try {
	            System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());
	            BufferedReader br=getReader(socket);
	            PrintWriter pw=getWriter(socket);
	            Waiter waiter = new Waiter(pw);
				waiter.printHelp();
				pw.println(Util.END);
				boolean exit = false;
				String command="";
				while (!exit) {
					System.out.println("waiting for command from client");
					command = br.readLine(); // 读入从client传来的字符串
					System.out.println("command recevied: " + command);
					exit = waiter.handle(command);
					pw.println(Util.END);
				}
	        } catch (IOException e) {
	            e.printStackTrace();
	        }finally{
	            try {
	                if(socket!=null)
	                    socket.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
			
		}
		
	}
}
